package com.hai.video;

import android.media.MediaPlayer;
import android.view.Surface;
import android.view.SurfaceHolder;

import java.io.IOException;

import tv.danmaku.ijk.media.player.IMediaPlayer;
import tv.danmaku.ijk.media.player.IjkMediaPlayer;

public class IjkPlayer implements IMediaApi {
    private IjkMediaPlayer ijkPlayer;

    public IjkPlayer() {
        ijkPlayer = new IjkMediaPlayer();
        configPlayer();
    }

    private void configPlayer() {
        //1：mediacodec硬解；0：mediacodec软解；
        ijkPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec", 1);
        ijkPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "start-on-prepared", 0);
//        ijkPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-avc", 1);
//        ijkPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-sync", 1);
//        ijkPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-auto-rotate", 1);
//        ijkPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-handle-resolution-change", 1);
//        ijkPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "mediacodec-hevc", 1);//开启hevc硬解
//        ijkPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_PLAYER, "max-fps", 30);//设置最大fps
//        ijkPlayer.setOption(IjkMediaPlayer.OPT_CATEGORY_FORMAT, "analyzeduration", 1);//设置播放前的探测时间 1,达到首屏秒开效果
//        ijkPlayer.setOption(1, "probesize", 200);  //播放前的探测Size，默认是1M, 改小一点会出画面更快
    }

    @Override
    public void setOnCompletionListener(MediaPlayer.OnCompletionListener listener) {
        ijkPlayer.setOnCompletionListener(new IMediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(IMediaPlayer iMediaPlayer) {
                if (listener != null) listener.onCompletion(null);
            }
        });
    }

    @Override
    public void setOnErrorListener(MediaPlayer.OnErrorListener listener) {
        ijkPlayer.setOnErrorListener(new IMediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(IMediaPlayer iMediaPlayer, int i, int i1) {
                if (listener != null) return listener.onError(null, i, i1);
                return false;
            }
        });
    }

    @Override
    public void setOnInfoListener(MediaPlayer.OnInfoListener listener) {
        ijkPlayer.setOnInfoListener(new IMediaPlayer.OnInfoListener() {
            @Override
            public boolean onInfo(IMediaPlayer iMediaPlayer, int i, int i1) {
                if (listener != null) {
                    return listener.onInfo(null, i, i1);
                }
                return false;
            }
        });
    }

    @Override
    public void setOnPreparedListener(MediaPlayer.OnPreparedListener listener) {
        ijkPlayer.setOnPreparedListener(new IMediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(IMediaPlayer iMediaPlayer) {
                if (listener != null) {
                    listener.onPrepared(null);
                }
            }
        });
    }

    @Override
    public void setOnSeekCompleteListener(MediaPlayer.OnSeekCompleteListener listener) {
        ijkPlayer.setOnSeekCompleteListener(new IMediaPlayer.OnSeekCompleteListener() {
            @Override
            public void onSeekComplete(IMediaPlayer iMediaPlayer) {
                if (listener != null) {
                    listener.onSeekComplete(null);
                }
            }
        });
    }

    @Override
    public void setOnVideoSizeChangedListener(MediaPlayer.OnVideoSizeChangedListener listener) {
        ijkPlayer.setOnVideoSizeChangedListener(new IMediaPlayer.OnVideoSizeChangedListener() {
            @Override
            public void onVideoSizeChanged(IMediaPlayer iMediaPlayer, int i, int i1, int i2, int i3) {
                if (listener != null) {
                    listener.onVideoSizeChanged(null, i - i3, i1 - i3);
                }
            }
        });
    }

    @Override
    public void setDataSource(String path) {
        try {
            ijkPlayer.setDataSource(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void seekTo(long position) {
        ijkPlayer.seekTo(position);
    }

    @Override
    public void prepare() {
        ijkPlayer.prepareAsync();
    }

    @Override
    public void start() {
        ijkPlayer.start();
    }

    @Override
    public void pause() {
        ijkPlayer.pause();
    }

    @Override
    public void stop() {
        ijkPlayer.stop();
    }

    @Override
    public void reset() {
        ijkPlayer.reset();
        configPlayer();
    }

    @Override
    public void release() {
        ijkPlayer.release();
    }

    @Override
    public boolean isPlaying() {
        return ijkPlayer.isPlaying();
    }

    @Override
    public boolean isPaused() {
        return ijkPlayer.getCurrentPosition() > 0 && !isPlaying();
    }

    @Override
    public int getVideoWidth() {
        return ijkPlayer.getVideoWidth();
    }

    @Override
    public int getVideoHeight() {
        return ijkPlayer.getVideoHeight();
    }

    @Override
    public long getProgress() {
        return ijkPlayer.getCurrentPosition();
    }

    @Override
    public long getDuration() {
        return ijkPlayer.getDuration();
    }

    @Override
    public void setDisplay(SurfaceHolder sh) {
        ijkPlayer.setDisplay(sh);
    }

    @Override
    public void setSurface(Surface surface) {
        ijkPlayer.setSurface(surface);
    }

    @Override
    public Object getMedia() {
        return ijkPlayer;
    }
}
